package org.zjx.command.login;

import org.zjx.command.CompositeCommand;
import org.zjx.command.CompositeCommandBuilder;
import org.zjx.command.basic.MoveCommand;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.core.IWindow;
import org.zjx.state.StateManager;

import java.awt.*;
import java.awt.image.BufferedImage;

public class LoginHomeCommand extends CompositeCommand {
    public LoginHomeCommand(IWindow window, StateManager stateManager) {
        super(window, stateManager);
    }

    @Override
    protected void initializeSubCommands() {
        CompositeCommandBuilder builder = new CompositeCommandBuilder(targetWindow, stateManager);

        CompositeCommand serverFlow = builder
                .find(LoginCommandConfig.Images.HOME_NEXT_IMAGE, LoginCommandConfig.Buttons.HOME_NEXT_BUTTON, "nextButtonPoint")
                .ifFound("nextButtonPoint", (b, context) -> {
                    // 使用熟悉的构建器语法添加系列指令
                    Point point = (Point) context.get("nextButtonPoint");
                    b.move(point.x, point.y)
                            .delay(30)
                            .click()
                            .delay(1000);
                })
                .build();

        add(new MoveCommand(targetWindow, 30, 30, stateManager));
        add(serverFlow);
    }

    public static boolean validate(IWindow window, BufferedImage screenshot) {
        FindImageCommand findImageCommand = new FindImageCommand(window,screenshot,LoginCommandConfig.Images.HOME_NEXT_IMAGE ,LoginCommandConfig.Buttons.HOME_NEXT_BUTTON);
        findImageCommand.execute();
        return findImageCommand.findImage();
    }
}
